;-------------------------------------------------------------LICENSE--------------------------------------------------------------;
;                                                                                                                                  ;
;The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    ;
;and Message Passing Interface (MPI) parallelization.                                                                              ;
;                                                                                                                                  ;
;Copyright (C) 2012                                                                                                                ;
;Ronglin Jiang                                                                                                                     ;
;rljiang@ssc.net.cn                                                                                                                ;
;585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             ;
;                                                                                                                                  ;
;This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         ;
;as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             ;
;                                                                                                                                  ;
;This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    ;
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        ;
;                                                                                                                                  ;
;You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     ;
;Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   ;
;                                                                                                                                  ;
;-------------------------------------------------------------LICENSE--------------------------------------------------------------;

pro map_plot_1d, VAR_NAME = var_name, MESH = mesh,                                                                                 $
   OUTPUTPATH = outputpath, DATAPATH = datapath, XSIZE = xsize, YSIZE = ysize, PS = ps, RGB_TABLE = rgb_table,                     $
   CHAR_COLOR = char_color, LINE_COLOR = line_color, CPU_COLOR = cpu_color, BACKGROUND = background,                               $
   PLOT_RANGE = plot_range, LOG = log, VAR_OUT = var_out, READDATA_ONLY = readdata_only,                                           $
   TIME_START = time_start, TIME_END = time_end, SWAP_ENDIAN = swap_endian, QUIET = QUIET

   if keyword_set(VAR_NAME) then begin
      var_flag = 1
   endif else begin
      var_flag = 0
      var_name = 'ro'
   endelse

   if not keyword_set(xsize) then xsize = 1200
   if not keyword_set(ysize) then ysize = 1200
   if not keyword_set(ps) then ps = 0
   if not keyword_set(log) then log = 0
   if not keyword_set(readdata_only) then readdata_only = 0
   if not keyword_set(swap_endian) then swap_endian = 0
   if not keyword_set(quiet) then quiet = 0
   if not keyword_set(mesh) then mesh = 0
   if n_elements(rgb_table) eq 0 then rgb_table = -1
   if n_elements(char_color) eq 0 then char_color = 0
   if n_elements(line_color) eq 0 then line_color = 255
   if n_elements(background) eq 0 then background = 255
   if n_elements(time_start) eq 0 then time_start = 0
   if n_elements(time_end) eq 0 then time_end = -1

   if quiet eq 0 then begin 
      print
      print, '|-------------------------------------------------------------------------|'
      print, ' Variable name: ', (var_flag eq 1)? var_name : 'none'
      print, ' PS? ', (ps eq 1)? 'Yes' : 'No'
      print, '|-------------------------------------------------------------------------|'
      print
   endif

;   var_total_name = [['ro', 'Density'], 
;                     ['gp', 'Gas pressure'], 
;                     ['mp', 'Magnetic pressure'], 
;                     ['te', 'Temperature'], 
;                     ['beta', 'Plasma beta'], 
;                     ['az', 'potential field'], 
;                     ['vx', 'Velocity (x)'], 
;                     ['vy', 'Velocity (y)'], 
;                     ['vz', 'Velocity (z)'], 
;                     ['bx', 'Gas pressure'], 
;                     ['by', 'Gas pressure'], 
;                     ['bz', 'Gas pressure'],  

   usr_var, xsize, ysize
   save_info, entry_info
   usr_font

   if rgb_table eq -1 then begin
      usr_color, color = color_type
   endif else begin
      device, decomposed = 0
      loadct, rgb_table
   endelse

   on_error, 2
   
   if file_test(datapath+'/par.mapvis') eq 0 then begin
      print
      print, ' Error:'
      print, ' Can not find file: par.mapvis'
      print
      retall
   endif

   cd, current = dir_old

;----------------------------------------------------------------------------------------------------------------------------------------------|
   close, /all

   directory = '' & n_dims = 0 & sys = 0 & ng = 0 & ini_block_x = 0  & cpu_number = 0 & output_real8_flag = 0
   nx_no_bnd = 0 & nx_block = 0 & nx_total = 0 & ratio = 0 & n_levels = 0
   div_clean_flag = 0 & output_level = 0
   nt = 0l & n_var = 0l

   openr, 1, datapath + '/par.mapvis'
   readf, 1, directory
   directory = strcompress(directory, /remove_all)
   readf, 1, ng
   readf, 1, nx_no_bnd
   readf, 1, nx_block
   readf, 1, nx_total
   readf, 1, ini_blocks
   readf, 1, ratio
   readf, 1, n_levels
   readf, 1, output_real8_flag
   readf, 1, output_level
   readf, 1, gm
   readf, 1, rangex
   readf, 1, zero_posx
   readf, 1, cfl
   readf, 1, nt
   t = dblarr(nt, /nozero)
   dt = dblarr(nt, /nozero)
   t_tmp = 0.0d0
   for i_t = 0, nt -1  do begin
      readf, 1, t_tmp
      t[i_t] = t_tmp
   endfor
   for i_t = 0, nt -1  do begin
      readf, 1, t_tmp
      dt[i_t] = t_tmp
   endfor
   readf, 1, n_var
   var_names = strarr(n_var)
   var_maxs = fltarr(n_var)
   var_mins = fltarr(n_var)
   var_dims = intarr(n_var)
   var_name_tmp = ''
   for i_var = 0, n_var - 1 do begin
      readf, 1, var_name_tmp
      var_names[i_var] = strcompress(strmid(var_name_tmp, 0, 12), /remove_all)
   endfor
   var_max_tmp = 0.0
   for i_var = 0, n_var - 1 do begin
      readf, 1, var_max_tmp
      var_maxs[i_var] = var_max_tmp
   endfor
   var_min_tmp = 0.0
   for i_var = 0, n_var - 1 do begin
      readf, 1, var_min_tmp
      var_mins[i_var] = var_min_tmp
   endfor
   var_dim_tmp = 0.0
   for i_var = 0, n_var - 1 do begin
      readf, 1, var_dim_tmp
      var_dims[i_var] = var_dim_tmp
      if var_dims[i_var] eq 3 then var_dims[i_var] = 2
   endfor
   close, 1

;----------------------------------------------------------------------------------------------------------------------------------|
   n_blocks = 0l
   dx = 0.0d0
   level = 0l
   nc = 0l

   nx_lv = (nx_total - ng * 2) * 2 ^ (output_level - 1) + 1

   x = dblarr(nx_no_bnd + 1, /nozero)
   x_lv = dblarr(nx_lv, /nozero)

   if output_real8_flag eq 8 then begin
      var_tmp = dblarr(nx_no_bnd + 1)
      var_lv_tmp = dblarr(nx_lv)
      vars = dblarr(nx_no_bnd + 1, total(var_dims), /nozero)
      vars_lv = dblarr(nx_lv, total(var_dims), /nozero)
   endif else begin
      var_tmp = fltarr(nx_no_bnd + 1)
      var_lv_tmp = fltarr(nx_lv)
      vars = fltarr(nx_no_bnd + 1, total(var_dims), /nozero)
      vars_lv = fltarr(nx_lv, total(var_dims), /nozero)
   endelse

   if n_elements(outputpath) then cd, outputpath

   if time_start gt nt then begin
      if quiet eq 0 then print, ' Warning: The given time_start is too large, which will be the last time.'
      time_start = nt
   endif

   if time_end gt nt then begin
      if quiet eq 0 then print, ' Warning: The given time_end is too large, which will be the last time.'
      time_end = nt 
   endif

   if time_start lt 1 then begin
      if quiet eq 0 then print, ' Warning: The given time_start is too small, which will be the first time.'
      time_start = 1
   endif
   if time_end lt 1 then begin
      if quiet eq 0 then print, ' Warning: The given time_end is too small, which will be the first time.'
      time_end = 1
   endif

;----------------------------------------------------------------------------------------------------------------------------------|
   for ti = time_start, time_end do begin

      datavis_name = datapath + '/data' + name_seq(strcompress(ti, /remove_all), '4.0', /nosymbol) + '.mapvis'
      openr, unit_data, datavis_name, /get_lun, /f77_unformatted, swap_endian = swap_endian

      if quiet eq 0 then print, ' Time = ', t[ti - 1]

      var_id = 0
      if readdata_only eq 0 then begin

         for i_var = 0, n_var - 1 do begin
            if var_name eq var_names[i_var] then begin
               title = strupcase(var_name)
               break
            endif
         endfor
         if i_var ge n_var then begin
            message, ' No this variable: ' + var_name
         endif

         var_id = i_var
         if var_flag eq 0 then title = 'novar'

         xrange = dblarr(2)
         yrange = dblarr(2)
   
         if keyword_set(plot_range) then begin
            xrange[0] = plot_range[0]
            xrange[1] = plot_range[1]
            yrange[0] = plot_range[2]
            yrange[1] = plot_range[3]
         endif else begin
            xrange[0] = 0.0d0 - zero_posx
            xrange[1] = rangex - zero_posx
            yrange[0] = var_mins[var_id]
            yrange[1] = var_maxs[var_id]
         endelse

         if mesh eq 1 then begin
            title = 'Level'
            yrange[0] = 0
            yrange[1] = n_levels + 1
         endif

;         if keyword_set(log) then yrange = alog10(yrange)

         file = strcompress(title, /remove_all) + '_' +                                                                            $
               'zoom' + strcompress(keyword_set(plot_range), /remove_all) + '_' +                                                  $
               'time' + name_seq(strcompress(t[ti - 1], /remove_all), '3.3')
         usr_plot_sys, entry_dev, image_name = file, xsize = xsize, ysize = ysize, /start, /pixmap, ps = ps

         plot, x_lv, var_lv_tmp, xstyle = 1, ystyle = 1, posi = [0.2, 0.20, 0.85, 0.85],                                           $
            background = background, color = char_color, title = 'Time=' + strmid(strcompress(t[ti - 1]), 1, 4),                   $
            xtitle = 'x', ytitle = title, xrange = xrange, yrange = yrange, nodata = 1, ylog = log,                                 $
            charsize = !usr_chars, thick = !usr_thick, charthick = !usr_chart, xthick = !usr_xthick, ythick = !usr_ythick

      endif

;----------------------------------------------------------------------------------------------------------------------------------|
      readu, unit_data, n_blocks

      lv_p = 1
      for i_block = 0, n_blocks - 1 do begin

         readu, unit_data, level, nc
         readu, unit_data, dx
         readu, unit_data, x

         for i_var = 0, total(var_dims) - 1 do begin
            readu, unit_data, var_tmp
            vars[*, i_var] = var_tmp
         endfor

         if nc eq 0 then begin
            if var_dims[var_id] eq 2 then begin
               print, 'The variable with dim = 3 is not avaliable.'
            endif else begin
               var = vars[*, total(var_dims[0:var_id])-1]
            endelse

            if mesh eq 1 then begin
               var[*] = level
               var[0] = lv_p
               lv_p = level
            endif
;----------------------------------------------------------------------------------------------------------------------------------|
            if readdata_only eq 0 then oplot, x, var, color = line_color, thick = !usr_thick

         endif
      endfor

      if readdata_only eq 0 then begin
         axis, xaxis = 0, xstyle = 1, color = char_color, xtick_get = x_tick,                                                      $
            charsize = !usr_chars, charthick = !usr_chart, xthick = !usr_xthick, ythick = !usr_ythick
         axis, yaxis = 0, ystyle = 1, color = char_color, ytick_get = y_tick,                                                      $
            charsize = !usr_chars, charthick = !usr_chart, xthick = !usr_xthick, ythick = !usr_ythick
         x_tick_len = n_elements(x_tick)
         y_tick_len = n_elements(y_tick)
         axis, xaxis = 1, xstyle = 1, color = char_color, xtickname = replicate(' ', x_tick_len),                                  $
            charsize = !usr_chars, charthick = !usr_chart, xthick = !usr_xthick, ythick = !usr_ythick
         axis, yaxis = 1, ystyle = 1, color = char_color, ytickname = replicate(' ', y_tick_len),                                  $
            charsize = !usr_chars, charthick = !usr_chart, xthick = !usr_xthick, ythick = !usr_ythick
      endif

      readu, unit_data, level, nc
      readu, unit_data, dx
      readu, unit_data, x_lv

      for i_var = 0, total(var_dims) - 1 do begin
         readu, unit_data, var_lv_tmp
         vars_lv[*, i_var] = var_lv_tmp
      endfor

;----------------------------------------------------------------------------------------------------------------------------------|
;     var output
;----------------------------------------------------------------------------------------------------------------------------------|
      var_out = create_struct('header', 'This is created by map_plot_2d of MAP code.')
      var_out = create_struct(var_out, 't', t[ti - 1])
      var_out = create_struct(var_out, 'x', x_lv)
      var_out = create_struct(var_out, 'nx', nx_lv)
      for i_var = 0, n_var - 1 do begin
         if var_dims[i_var] eq 2 then begin
            var_out = create_struct(var_out, var_names[i_var], vars_lv[*, floor(total(var_dims[0:i_var])-2) :              $
                                                                             floor(total(var_dims[0:i_var])-1)])
         endif else begin
            var_out = create_struct(var_out, var_names[i_var], vars_lv[*, floor(total(var_dims[0:i_var])-1)])
         endelse
      endfor

      if readdata_only ne 0 then continue
;----------------------------------------------------------------------------------------------------------------------------------|
      usr_plot_sys, entry_dev, image_name = file, /done, ps = ps
      close, unit_data
      free_lun, unit_data
   endfor

;----------------------------------------------------------------------------------------------------------------------------------|
   cd, dir_old
   reset_info, entry_info
end

